#include <cubefs/api/api.hpp>

#include <map>

#define CUBEFS_SOURCE "api-impl/all/container.cpp:" CUBEFS_AT

typedef cubefs::api::Container* (*ContainerFactoryFn)();

namespace cubefs{ namespace api {

using namespace std;
using namespace cubefs::tools;

Container* Container::Create(StringRef name, StringRef version, Int type)
{
	if (HasPlugins())
	{
		String container_type;

		if (type == Container::CONTAINER)
		{
			container_type = "Container";
		}
		else if (type == Container::PAGE_MANAGER)
		{
			container_type = "PageManager";
		}
		else {
			throw CubefsException(CUBEFS_SOURCE, "Unknown container type", type);
		}

		Plugin* plugin = Plugin::FindPlugin(container_type, name, Version(version), GetCubefsApiVersion());
		if (plugin != NULL)
		{
			SharedLibrary& library = plugin->GetLibrary();
			library.Open();

			ContainerFactoryFn factory = (ContainerFactoryFn)library.GetSymbolAddress("Create"/* + name + container_type*/);

			return factory();
		}
		else {
			throw CubefsException(CUBEFS_SOURCE, "Can't create container with name=" + name);
		}
	}
	else {
		return NULL;
	}
}

void InitContainersMetadata() {

}

}}

#undef CUBEFS_SOURCE

